<template>
  <view class="bd">
    <view class="inp-group" style="">
      <view class="inp-item uni-item-line flex flex-al-c">
        <view class="inp-title" style="width: 200upx">真实姓名</view>
        <input
          class="inp"
          type="text"
          placeholder="请输入姓名"
          v-model="userName"
          maxlength="13"
        />
      </view>
      <view class="inp-item uni-item-line flex flex-al-c">
        <view class="inp-title" style="width: 200upx">性别</view>
        <input
          class="inp"
          type="text"
          @click="show = true"
          disabled
          v-model="selectorObj[selectorObjIndex].name"
        />
        <u-picker
          v-model="show"
          mode="selector"
          :range="selectorObj"
          range-key="name"
          @confirm="getSelectVal"
        ></u-picker>
      </view>
      <view class="inp-item uni-item-line flex flex-al-c">
        <view class="inp-title" style="width: 200upx">证件类型</view>
        <input
          class="inp"
          type="text"
          @click="show1 = true"
          disabled
          v-model="selectorObj1[selectorObjIndex1].name"
        />
        <u-picker
          v-model="show1"
          mode="selector"
          :range="selectorObj1"
          range-key="name"
          @confirm="getSelectVal1"
        ></u-picker>
      </view>
      <view class="inp-item uni-item-line flex flex-al-c" v-if="sel1">
        <view class="inp-title" style="width: 200upx">身份证号</view>
        <input
          class="inp"
          type="text"
          placeholder="请输入您的身份证号"
          v-model="idCard"
          maxlength="18"
        />
      </view>
      <view class="inp-item uni-item-line flex flex-al-c" v-if="sel2">
        <view class="inp-title" style="width: 200upx">出生证号</view>
        <input
          class="inp"
          type="text"
          placeholder="请输入您的出生证号"
          v-model="idCsCard"
          maxlength="10"
        />
      </view>
      <view
        class="inp-item uni-item-line flex flex-al-c"
        @click="selcal"
        v-if="sel2"
      >
        <view class="inp-title" style="width: 260upx">请选择生日</view>
        <input class="inp" type="text" disabled v-model="findate" />
		<u-picker v-model="showcal" mode="time" @confirm="change"></u-picker>
      </view>
      <view v-if="subType == '1'">
        <view class="inp-item uni-item-line flex flex-al-c">
          <view class="inp-title" style="width: 200upx">手机号</view>
          <input
            class="inp"
            type="number"
            placeholder="请输入手机号"
            v-model="phone"
            maxlength="11"
          />
        </view>
        <view class="inp-item uni-item-line flex flex-al-c">
          <view class="inp-title" style="width: 200upx">关系</view>
          <input
            class="inp"
            type="text"
            @click="show2 = true"
            disabled
            v-model="yblist[selectorObjIndex2].name"
          />
          <u-picker
            v-model="show2"
            mode="selector"
            :range="yblist"
            range-key="name"
            @confirm="getSelectVal2"
          ></u-picker>
        </view>
      </view>
      <view style="margin-top: 100upx">
        <button type="default" class="sbtn" @tap="sub">确认提交</button>
      </view>
      <u-modal
        v-model="showtip"
        z-index="2000"
        :content="content"
        :show-cancel-button="true"
        @confirm="gouser"
      ></u-modal>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      selectorObj: [
        {
          name: "男",
          id: "1",
        },
        {
          name: "女",
          id: "0",
        },
      ],
      selectorObj1: [
        {
          name: "居民身份证",
          id: "1",
        },
        {
          name: "出生医学证",
          id: "0",
        },
      ],
      show: false,
      show1: false,
      showcal: false,
      mode: "date",
      selectorObjIndex: 0,
      selectorObjIndex1: 0,
      showtip: false,
      content: "该用户已被注册，是否关联其所有信息",

      selectorObj2: [
        {
          name: "爸爸",
          id: "0",
        },
        {
          name: "妈妈",
          id: "1",
        },
      ],
      show2: false,
      selectorObjIndex2: 0,
      userlist: "",
      userName: "",
      idCard: "", //身份证号
      idCsCard: "", //出生证号
      phone: "",
      realtion: "",
      findate: "", //选择的出生日期
      sel1: true,
      sel2: false,
      age: "", //出生证年龄
    };
  },
  onLoad(options) {
    this.getInsType();
    this.subType = options.subType; // 0完善个人信息 1添加联系人信息
    // this.subType = 1; // 0完善个人信息 1添加联系人信息
  },
  computed: {
    yblist() {
      let list = [];
      this.selectorObj2.forEach((item) => {
        let name = item.dictLabel;
        let id = item.dictValue;
        list.push({
          name: name,
          id: id,
        });
      });
      return list;
    },
  },
  onReady() {
    // this.getUiStyle();
  },
  methods: {
    getUiStyle() {
      var style = plus.navigator.getUiStyle();
      if ("dark" == style) {
        console.log("当前为暗黑模式");
        uni.setTabBarStyle({
          backgroundColor: "#1B1C1E",
        });
        uni.setNavigationBarColor({
          frontColor: "#ffffff",
          backgroundColor: "#1B1C1E",
          animation: {
            duration: 400,
            timingFunc: "easeIn",
          },
        });
      } else {
        console.log("当前为普通模式");
      }
    },
    change(e) {
		console.log(e)
      this.findate = `${e.year}-${e.month}-${e.day}`
    },
    selcal() {
      this.showcal = true;
    },
    getSelectVal(v) {
      this.selectorObjIndex = v[0];
    },
    getSelectVal1(v) {
      this.selectorObjIndex1 = v[0];
      //console.log(this.selectorObj1[v].id)
      if (this.selectorObj1[v].id == 0) {
        console.log(v);
        //0 出生证  1 身份证
        this.sel1 = false;
        this.sel2 = true;
        this.idCard = "";
      } else {
        this.sel1 = true;
        this.sel2 = false;
        this.idCsCard = "";
      }
    },
    getInsType() {
      let self = this;
      // this.config.loading()
      this.config
        .get("/util/sysDictData/type/family_relation", {})
        .then((res) => {
          uni.hideLoading();
          if (res.data.code === 200) {
            self.selectorObj2 = res.data.data;
          } else {
            if (res.data.code == "-201") {
              // self.config.handleError(res.data);
            } else {
              uni.showToast({
                title: self.config.handleError(res.data),
                icon: "none",
              });
            }
          }
        });
    },
    getSelectVal(v) {
      this.selectorObjIndex = v[0];
    },
    getSelectVal2(v) {
      this.selectorObjIndex2 = v[0];
      this.realtion = this.selectorObj2[v].dictValue;
    },
    onBackPress(options) {
      if (this.subType == 1) {
        return false;
      }
    },
    valid() {
      if (!this.userName) {
        uni.showToast({
          title: "请输入姓名",
          icon: "none",
        });
        return false;
      }
      // if (!this.config.pub._isIdCard(this.idCard)) {
      //   uni.showToast({
      //     title: "请输入正确的身份证号",
      //     icon: "none",
      //   });
      //   return false;
      // }
      if (!this.$u.test.idCard(this.idCard) && this.sel1) {
        uni.showToast({
          title: "请输入正确的身份证号",
          icon: "none",
        });
        return false;
      }
      if (this.config.getage(this.idCard) < 0) {
        uni.showToast({
          title: "请输入正确的身份证号",
          icon: "none",
        });
        return false;
      }
      if (!this.idCsCard && this.sel2) {
        uni.showToast({
          title: "请输入出生证号",
          icon: "none",
        });
        return false;
      }
      if (!this.findate && this.sel2) {
        uni.showToast({
          title: "请选择生日",
          icon: "none",
        });
        return false;
      }
      if (this.subType == 1) {
        if (!this.phone) {
          uni.showToast({ title: "请输入手机号", icon: "none" });
          return false;
        }
      }
      return true;
    },
    sub() {
      if (!this.valid()) return;
      console.log(
        this.userName,
        this.selectorObj[this.selectorObjIndex].id,
        this.idCard
      );
      let uinfo = this.config.pub.analyzeIDCard(this.idCard);
      console.log(uinfo.sex, this.selectorObj[this.selectorObjIndex].id);
      if (
        uinfo.sex != this.selectorObj[this.selectorObjIndex].id &&
        this.sel1
      ) {
        uni.showToast({
          title: "所选性别与身份证不符",
          icon: "none",
        });
        return;
      }
      if (this.sel2) {
        let uinfoage = this.config.pub.compage(this.findate);
        console.log(uinfoage.age);
        this.age = uinfoage.age;
      }
      // 个人信息
      let user = {
        userName: this.userName,
        gender: this.selectorObj[this.selectorObjIndex].id,
        idCard: this.idCard,
        birthCertificate: this.idCsCard,
        birthday: this.findate,
        age: uinfo.age || this.age,
        gravid: "0",
        finalMenstruationTime: "",
        chronic: "0",
        bloodPressureDown: "",
        bloodPressureUp: "",
        bloodFat: "",
        realtion: this.realtion,
      };
      this.userlist = user;
      this.config
        .post("/family/checkUserRelation", this.userlist)
        .then((res) => {
          //console.log(res)
          if (res.data.code === 200) {
            if (uni.getStorageSync("jscid") == 1) {
              this.sendCid();
            }
            if (res.data.data.code === 1) {
              if (this.subType == "1") {
                user.subType = "1"; // 完善个人信息/添加联系人信息
                user.phone = this.phone;
                user.realtion = this.realtion;
              }
              if (uinfo.age <= 7) {
                // 儿童
                uni.navigateTo({
                  url: "/pages/reg/answer?user=" + JSON.stringify(user),
                });
              } else {
                if (this.selectorObj[this.selectorObjIndex].id == "1") {
                  // 男性
                  if (uinfo.age >= 35) {
                    // 慢性病
                    uni.navigateTo({
                      url: "/pages/reg/disease?user=" + JSON.stringify(user),
                    });
                  } else {
                    uni.navigateTo({
                      url: "/pages/reg/answer?user=" + JSON.stringify(user),
                    });
                  }
                } else {
                  // 女性
                  if (uinfo.age > 49) {
                    uni.navigateTo({
                      url: "/pages/reg/disease?user=" + JSON.stringify(user),
                    });
                  } else if (uinfo.age >= 18 && uinfo.age <= 49) {
                    // 怀孕年龄段
                    uni.navigateTo({
                      url: "/pages/reg/pregnancy?user=" + JSON.stringify(user),
                    });
                  } else {
                    uni.navigateTo({
                      url: "/pages/reg/answer?user=" + JSON.stringify(user),
                    });
                  }
                }
              }
            } else {
              this.showtip = true;
            }
          } else {
            uni.showToast({
              title: self.config.handleError(res),
              icon: "none",
            });
          }
        });
    },
    sendCid() {
      this.config
        .post("/uni/saveCidToUser", {
          cid: uni.getStorageSync("cid"),
          ptType: uni.getStorageSync("device"),
        })
        .then((res) => {
          console.log(res);
        });
    },
    gouser() {
      this.config.post("/family/GlUserRelation", this.userlist).then((res) => {
        //console.log(res)
        if (res.data.code === 200) {
          uni.switchTab({ url: "/pages/home/home" });
        } else {
          uni.showToast({
            title: "该用户已存在家庭组",
            icon: "none",
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
// 表单
.inp-group {
  width: 85%;
  margin: 0 auto;

  .inp-item {
    margin-top: 10upx;

    .inp-title {
      padding: 15upx 0;
      margin-top: 30upx;
      font-size: 30upx;
      font-weight: 900;
      color: #777;
    }

    .inp {
      padding: 15upx 0;
      color: #333;
      margin-top: 30upx;
    }
  }

  .sbtn {
    background: #18c7e1;
    color: #fff;
  }
}
</style>
<style scoped>
@media (prefers-color-scheme: closedark) {
  .bd {
    background-color: #1b1c1e !important;
    height: 100vh;
  }
  .bd1 {
    background-color: #38383c !important;
  }
  .t1 {
    color: #ccc !important;
  }
  .inp-title {
    color: #ccc !important;
  }
  >>> input {
    color: #ccc !important;
  }
}
</style>